package com.xbongbong.paas.handle;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.service.CallCenterCommonService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.pro.product.pojo.vo.RelativeProductExplainVO;
import com.xbongbong.pro.weblist.pojo.ListOptionPackagePojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.CustomerFocusEntity;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.help.SaasListHelp;
import com.xbongbong.saas.model.CustomerFocusModel;
import com.xbongbong.saas.model.TagLinkModel;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 客户操作方式
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/27 11:23
 * @since v1.0
 */
@Component
public class DistributorContactOptionHandle extends AbstractOptionHandle {


    @Resource
    private CallCenterCommonService callCenterCommonService;
    @Resource
    private SaasListHelp saasListHelp;
    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private CustomerFocusModel customerFocusModel;
    @Resource
    private TagLinkModel tagLinkModel;

    @Override
    public void setOptionsInExplain(FormDataListDTO formDataListDTO, List<FieldAttrEntity> explainList, PaasFormDataEsListVO paasFormDataEsListVO) throws XbbException {
        // 去掉子表单标题头，实际上可以加在HandleExplainHelp.handleExplain中，但是避免对其他业务造成影响，先写在这里，如果全部列表改造的话，可统一改到HandleExplainHelp.handleExplain中
        removeSubFormItems(explainList, formDataListDTO.getBusinessType());
    }


    @Override
    public void setOptionsInData(FormDataListDTO formDataListDTO, PaasFormDataEsListVO paasFormDataEsListVO) throws XbbException {
        List<PaasFormDataEntityExt> dataList = paasFormDataEsListVO.getPaasFormDataESList();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(paasFormDataEsListVO.getHeadList().size());
        paasFormDataEsListVO.getHeadList().forEach(item->{
            explainMap.put(item.getAttr(), item);
        });
        ListOptionPackagePojo listOptionPackagePojo = getLastOptionData(formDataListDTO);
        for (PaasFormDataEntityExt item : dataList) {
            saasListHelp.setOptionValue(item, formDataListDTO, listOptionPackagePojo, formDataListDTO.getBusinessType(),
                    formDataListDTO.getSubBusinessType(), formDataListDTO.getListGroupId(), explainMap, new RelativeProductExplainVO());
        }

    }

    /**
     * 增加关注和提醒，以及 “退 ” 标签 “归标签”
     * @param formDataListDTO
     * @return
     * @throws XbbException
     */
    public ListOptionPackagePojo getFirstOption(FormDataListDTO formDataListDTO) throws XbbException {
        ListOptionPackagePojo listOptionPackagePojo = new ListOptionPackagePojo();
        // 关注
        listOptionPackagePojo.setNeedOption1(true);
        //提醒
        listOptionPackagePojo.setNeedOption2(true);
        listOptionPackagePojo.setArchivedTag(CustomerManagementEnum.NAME.getAttr() + "Tag");
        listOptionPackagePojo.setPublicTag(CustomerManagementEnum.NAME.getAttr() + "Tag");
        return listOptionPackagePojo;
    }

    private ListOptionPackagePojo getLastOption(FormDataListDTO formDataListDTO) throws XbbException {
        String corpid = formDataListDTO.getCorpid();
        String userId = formDataListDTO.getUserId();
        ListOptionPackagePojo listOptionPackagePojo = new ListOptionPackagePojo();
        // 判断是否有呼叫权限
        boolean isActiveCall = callCenterCommonService.isOpenCallAndAgent(corpid, userId);
        if (isActiveCall) {
            listOptionPackagePojo.setNeedOption3(true);
        }
        //跟进
        listOptionPackagePojo.setNeedOption4(formDataListDTO.getLoginUser().getPermSet().contains(ProPermissionAliasEnum.CUSTOMER_COMMUNICATE_VIEW.getAlias()));
        return listOptionPackagePojo;
    }
    @Override
    public ListOptionPackagePojo getFirstOptionData(FormDataListDTO formDataListDTO, PaasFormDataEsListVO paasFormDataEsListVO) throws XbbException {
        // 获取第一个接口的按钮和标签
        ListOptionPackagePojo listOptionPackagePojo = getFirstOption(formDataListDTO);
        String corpid = formDataListDTO.getCorpid();
        Integer businessType = formDataListDTO.getBusinessType();
        List<Long> dataIdList = new ArrayList<>();
        paasFormDataEsListVO.getPaasFormDataESList().forEach(item->{
            dataIdList.add(item.getDataId());
        });
        listOptionPackagePojo.setNeedOption1(true);
        if (dataIdList.isEmpty()) {
            return listOptionPackagePojo;
        }
        // 提醒
        saasListHelp.getPushNotify(formDataListDTO.getIdIn(), corpid, businessType, listOptionPackagePojo.getPushNotifyList());
        //关注
        boolean isCusShare;
        try {
            isCusShare = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.SHARE_FOCUS_CUSTOEMR.getAlias(), BasicConstant.IS_USE.toString());
        } catch (XbbException e) {
            isCusShare = Objects.equals(CompanyConfigEnum.SHARE_FOCUS_CUSTOEMR.getValue(), BasicConstant.IS_USE.toString());
        }

        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        if(!isCusShare) {
            map.put("userId", formDataListDTO.getUserId());
        }
        map.put("customerIdIn", dataIdList);
        map.put("del", 0);
        map.put("focus", 1);
        List<CustomerFocusEntity> customerFocusEntityList = customerFocusModel.findEntitys(map);
        for (CustomerFocusEntity entity : customerFocusEntityList) {
            listOptionPackagePojo.getFocusList().add(entity.getCustomerId());
        }
        // 退回公海池
        listOptionPackagePojo.setPublicTagMap(tagLinkModel.getPublicTag(corpid, dataIdList));
        return listOptionPackagePojo;
    }

    private ListOptionPackagePojo getLastOptionData(FormDataListDTO formDataListDTO) throws XbbException {
        ListOptionPackagePojo listOptionPackagePojo = getLastOption(formDataListDTO);
        String corpid = formDataListDTO.getCorpid();
        // 跟进记录
        if (listOptionPackagePojo.getNeedOption4()) {
            saasListHelp.getCommunicate(formDataListDTO, corpid, listOptionPackagePojo, formDataListDTO.getIdIn());
        }
        //关注
        boolean isCusShare;
        try {
            isCusShare = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.SHARE_FOCUS_CUSTOEMR.getAlias(), BasicConstant.IS_USE.toString());
        } catch (XbbException e) {
            isCusShare = Objects.equals(CompanyConfigEnum.SHARE_FOCUS_CUSTOEMR.getValue(), BasicConstant.IS_USE.toString());
        }
        listOptionPackagePojo.setNeedOption1(true);
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        if(!isCusShare) {
            map.put("userId", formDataListDTO.getUserId());
        }
        map.put("customerIdIn", formDataListDTO.getIdIn());
        map.put("del", 0);
        map.put("focus", 1);
        List<CustomerFocusEntity> customerFocusEntityList = customerFocusModel.findEntitys(map);
        for (CustomerFocusEntity entity : customerFocusEntityList) {
            listOptionPackagePojo.getFocusList().add(entity.getCustomerId());
        }
        return listOptionPackagePojo;
    }
}
